Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | 'use client'; import React from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Progress } from '@/components/ui/progress'; import { AlertTriangle, User, Smartphone, Settings, CheckCircle, XCircle } from 'lucide-react'; import { DeviceStats } from '@/services/device'; import { userService } from '@/services/user'; import { useTranslation } from 'react-i18next'; import useLoadNamespace from '@/hooks/useLoadNamespace'; interface DeviceLimitWarningProps { deviceStats?: DeviceStats; isLoading: boolean; } interface UserDeviceInfo { user_id: number; username: string; device_count: number; max_devices: number; usage_percentage: number; warning_level: 'safe' | 'warning' | 'critical' | 'exceeded'; } export default function DeviceLimitWarning({ deviceStats, isLoading }: DeviceLimitWarningProps) { // const [selectedUser, setSelectedUser] = useState<UserDeviceInfo | null>(null); const queryClient = useQueryClient(); useLoadNamespace('admin/devices'); const { t } = useTranslation('admin/devices'); // Update user device limit mutation const updateDeviceLimitMutation = useMutation({ mutationFn: async ({ userId, maxDevices }: { userId: number; maxDevices: number }) => { const result = await userService.updateUser(userId, { max_devices: maxDevices }); if (result.success) { return result.data; } throw new Error(result.error.details); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['device-stats'] }); queryClient.invalidateQueries({ queryKey: ['all-devices'] }); // setSelectedUser(null); }}); const getWarningLevel = (deviceCount: number, maxDevices: number): UserDeviceInfo['warning_level'] => { const percentage = (deviceCount / maxDevices) * 100; if (deviceCount > maxDevices) return 'exceeded'; if (percentage >= 100) return 'critical'; if (percentage >= 80) return 'warning'; return 'safe'; }; const getWarningBadge = (level: UserDeviceInfo['warning_level']) => { switch (level) { case 'safe': return ( <Badge variant="secondary" className="flex items-center gap-1"> <CheckCircle className="h-3 w-3" /> {t('devices.limits.badges.safe')} </Badge> ); case 'warning': return ( <Badge variant="outline" className="flex items-center gap-1 border-yellow-500 text-yellow-700"> <AlertTriangle className="h-3 w-3" /> {t('devices.limits.badges.warning')} </Badge> ); case 'critical': return ( <Badge variant="destructive" className="flex items-center gap-1"> <AlertTriangle className="h-3 w-3" /> {t('devices.limits.badges.critical')} </Badge> ); case 'exceeded': return ( <Badge variant="destructive" className="flex items-center gap-1"> <XCircle className="h-3 w-3" /> {t('devices.limits.badges.exceeded')} </Badge> ); } }; // const getProgressColor = (level: UserDeviceInfo['warning_level']) => { // switch (level) { // case 'safe': // return 'bg-green-500'; // case 'warning': // return 'bg-yellow-500'; // case 'critical': // case 'exceeded': // return 'bg-red-500'; // } // }; const handleIncreaseLimit = (user: UserDeviceInfo) => { updateDeviceLimitMutation.mutate({ userId: user.user_id, maxDevices: user.max_devices + 1}); }; // Process device stats into user device info const userDeviceInfo: UserDeviceInfo[] = deviceStats?.devices_by_user?.map(user => { const usagePercentage = (user.device_count / user.max_devices) * 100; const warningLevel = getWarningLevel(user.device_count, user.max_devices); return { user_id: user.user_id, username: user.username, device_count: user.device_count, max_devices: user.max_devices, usage_percentage: Math.min(usagePercentage, 100), warning_level: warningLevel}; }) || []; // Filter users that need attention (warning, critical, or exceeded) const usersNeedingAttention = userDeviceInfo.filter(user => user.warning_level !== 'safe' ); // Sort by warning level priority const sortedUsers = usersNeedingAttention.sort((a, b) => { const levelPriority = { exceeded: 4, critical: 3, warning: 2, safe: 1 }; return levelPriority[b.warning_level] - levelPriority[a.warning_level]; }); if (isLoading) { return ( <Card> <CardHeader> <CardTitle>{t('devices.limits.title')}</CardTitle> <CardDescription> {t('devices.limits.subtitle')} </CardDescription> </CardHeader> <CardContent> <div className="flex items-center justify-center py-8"> <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900"></div> </div> </CardContent> </Card> ); } return ( <Card> <CardHeader> <CardTitle>{t('devices.limits.title')}</CardTitle> <CardDescription> {t('devices.limits.subtitle')} </CardDescription> </CardHeader> <CardContent> {/* Summary Cards */} <div className="grid gap-4 md:grid-cols-3 mb-6"> <Card> <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> <CardTitle className="text-sm font-medium">{t('devices.limits.summary.warningTitle')}</CardTitle> <AlertTriangle className="h-4 w-4 text-yellow-600" /> </CardHeader> <CardContent> <div className="text-2xl font-bold"> {userDeviceInfo.filter(u => u.warning_level === 'warning').length} </div> <p className="text-xs text-muted-foreground"> {t('devices.limits.summary.warningDescription')} </p> </CardContent> </Card> <Card> <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> <CardTitle className="text-sm font-medium">{t('devices.limits.summary.criticalTitle')}</CardTitle> <AlertTriangle className="h-4 w-4 text-red-600" /> </CardHeader> <CardContent> <div className="text-2xl font-bold"> {userDeviceInfo.filter(u => u.warning_level === 'critical').length} </div> <p className="text-xs text-muted-foreground"> {t('devices.limits.summary.criticalDescription')} </p> </CardContent> </Card> <Card> <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2"> <CardTitle className="text-sm font-medium">{t('devices.limits.summary.exceededTitle')}</CardTitle> <XCircle className="h-4 w-4 text-red-600" /> </CardHeader> <CardContent> <div className="text-2xl font-bold"> {userDeviceInfo.filter(u => u.warning_level === 'exceeded').length} </div> <p className="text-xs text-muted-foreground"> {t('devices.limits.summary.exceededDescription')} </p> </CardContent> </Card> </div> {/* Users Needing Attention */} {sortedUsers.length > 0 ? ( <div className="space-y-4"> <h3 className="text-lg font-medium">{t('devices.limits.attention.title')}</h3> <Table> <TableHeader> <TableRow> <TableHead>{t('devices.table.user')}</TableHead> <TableHead>{t('devices.limits.table.deviceUsage')}</TableHead> <TableHead>{t('devices.limits.table.progress')}</TableHead> <TableHead>{t('devices.limits.table.warningLevel')}</TableHead> <TableHead>{t('devices.common.actions')}</TableHead> </TableRow> </TableHeader> <TableBody> {sortedUsers.map((user) => ( <TableRow key={user.user_id}> <TableCell> <div className="flex items-center gap-2"> <User className="h-4 w-4 text-muted-foreground" /> <span className="font-medium">{user.username}</span> </div> </TableCell> <TableCell> <div className="flex items-center gap-2"> <Smartphone className="h-4 w-4 text-muted-foreground" /> <span className="font-medium"> {t('devices.overview.deviceCount', { count: user.device_count, max: user.max_devices })} </span> <span className="text-sm text-muted-foreground"> ({user.usage_percentage.toFixed(0)}%) </span> </div> </TableCell> <TableCell> <div className="w-full"> <Progress value={user.usage_percentage} className="w-full h-2" /> </div> </TableCell> <TableCell> {getWarningBadge(user.warning_level)} </TableCell> <TableCell> <div className="flex items-center gap-2"> {user.warning_level === 'exceeded' || user.warning_level === 'critical' ? ( <Button variant="outline" size="sm" onClick={() => handleIncreaseLimit(user)} disabled={updateDeviceLimitMutation.isPending} > <Settings className="h-3 w-3 mr-1" /> {t('devices.limits.actions.increaseLimit')} </Button> ) : ( <Button variant="ghost" size="sm" disabled > {t('devices.limits.actions.monitor')} </Button> )} </div> </TableCell> </TableRow> ))} </TableBody> </Table> </div> ) : ( <div className="text-center py-8"> <CheckCircle className="h-12 w-12 text-green-500 mx-auto mb-4" /> <h3 className="text-lg font-medium text-gray-900 mb-2"> {t('devices.limits.allSafeTitle')} </h3> <p className="text-gray-500"> {t('devices.limits.allSafeDescription')} </p> </div> )} {/* All Users Overview */} {userDeviceInfo.length > 0 && ( <div className="mt-8 space-y-4"> <h3 className="text-lg font-medium">{t('devices.limits.allUsersTitle')}</h3> <div className="grid gap-2"> {userDeviceInfo.map((user) => ( <div key={user.user_id} className="flex items-center justify-between p-3 border rounded-lg"> <div className="flex items-center gap-3"> <User className="h-4 w-4 text-muted-foreground" /> <span className="font-medium">{user.username}</span> <span className="text-sm text-muted-foreground"> {t('devices.overview.deviceCount', { count: user.device_count, max: user.max_devices })} </span> </div> <div className="flex items-center gap-3"> <div className="w-24"> <Progress value={user.usage_percentage} className="h-2" /> </div> {getWarningBadge(user.warning_level)} </div> </div> ))} </div> </div> )} {/* Error Display */} {updateDeviceLimitMutation.error && ( <Alert variant="destructive" className="mt-4"> <AlertTriangle className="h-4 w-4" /> <AlertDescription> {updateDeviceLimitMutation.error.message} </AlertDescription> </Alert> )} </CardContent> </Card> ); } |